package com.project.lk.nurseapp.common.view.chat;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.cjt2325.cameralibrary.JCameraView;
import com.cjt2325.cameralibrary.listener.ClickListener;
import com.cjt2325.cameralibrary.listener.ErrorListener;
import com.cjt2325.cameralibrary.listener.JCameraListener;
import com.cjt2325.cameralibrary.util.DeviceUtil;
import com.cjt2325.cameralibrary.util.FileUtil;
import com.project.lk.nurseapp.R;
import com.project.lk.nurseapp.home.MainActivity;
import com.project.lk.nurseapp.utils.AppManager;
import com.project.lk.nurseapp.utils.ToastUtils;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
/**
 * 功能介绍：
 * 项目名：
 * 模块名称：
 * 作者：LK
 * 日期：2018/1/9
 * 修改日期：
 */
public class CameraViewActivity extends AppCompatActivity {


    @BindView(R.id.jcameraview)
    JCameraView jcameraview;
    private Unbinder mBind;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.activity_camera_view);
        //绑定黄油刀
        mBind = ButterKnife.bind(this);
        if (Build.VERSION.SDK_INT >= 19) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        } else {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(option);
        }
        AppManager.getAppManager().addActivity(this);

        init();
    }

    private void init() {
        jcameraview = (JCameraView) findViewById(R.id.jcameraview);
        //设置视频保存路径
        jcameraview.setSaveVideoPath(Environment.getExternalStorageDirectory().getPath() + File.separator + "JCamera");
        jcameraview.setTip("长按摄像");
        jcameraview.setFeatures(JCameraView.BUTTON_STATE_ONLY_RECORDER);
        jcameraview.setMediaQuality(JCameraView.MEDIA_QUALITY_MIDDLE);
        jcameraview.setErrorLisenter(new ErrorListener() {
            @Override
            public void onError() {
                //错误监听
                Log.e("MLOG", "camera error");
//                Intent intent = new Intent();
//                setResult(103, intent);
                AppManager.getAppManager().finishActivity();
            }

            @Override
            public void AudioPermissionError() {
                AppManager.getAppManager().finishActivity();
            }
        });
        //JCameraView监听
        jcameraview.setJCameraLisenter(new JCameraListener() {
            @Override
            public void captureSuccess(Bitmap bitmap) {
                //获取图片bitmap
//                Log.i("JCameraView", "bitmap = " + bitmap.getWidth());
                String path = FileUtil.saveBitmap("JCamera", bitmap);
                Intent intent = new Intent();
                intent.putExtra("path", path);
                setResult(101, intent);
                AppManager.getAppManager().finishActivity();
            }

            @Override
            public void recordSuccess(String url, Bitmap firstFrame) {
                //获取视频路径
                String path = FileUtil.saveBitmap("JCamera", firstFrame);
                Log.e("MLOG", "url = " + url + ", Bitmap = " + path);
                Intent intent = new Intent();
                intent.putExtra("video", url);
                intent.putExtra("imgPath",path);
                setResult(RESULT_OK, intent);
                AppManager.getAppManager().finishActivity();
            }
        });

        jcameraview.setLeftClickListener(new ClickListener() {
            @Override
            public void onClick() {
                AppManager.getAppManager().finishActivity();
            }
        });
//        jCameraView.setRightClickListener(new ClickListener() {
//            @Override
//            public void onClick() {
//                Toast.makeText(CameraActivity.this,"Right",Toast.LENGTH_SHORT).show();
//            }
//        });

        Log.i("MLOG", DeviceUtil.getDeviceModel());
    }
    @Override
    protected void onResume() {
        super.onResume();
        jcameraview.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        jcameraview.onPause();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (mBind!=null){
            mBind.unbind();
        }
    }

    @Override
    public void onBackPressed() {
        AppManager.getAppManager().finishActivity();
    }
}
